package com.huluobo.jetpackkotlindemo.hilt

import android.os.Bundle
import android.util.Log
import com.huluobo.jetpackkotlindemo.databinding.ActivityHiltBinding
import com.huluobo.jetpackkotlindemo.viewbinding.BaseActivity
import com.squareup.okhttp.Call
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import dagger.hilt.android.AndroidEntryPoint
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit
import javax.inject.Inject

/**
 * Created by lc on 2022/12/14.
 * Dagger(匕首) Hilt(刀柄) 同样都是依赖注入的框架,Dagger是通过反射实现的依赖注入,性能上有问题,Hilt可以说是Dagger的升级版
 *
 * 1.给Activity添加@AndroidEntryPoint注解
 * 2.声明一个延迟初始化的UserManager变量,同时给他添加一个@Inject
 * 3.这样Activity中可以直接通过依赖注入获取到了UserManager的对象
 */
@AndroidEntryPoint
class HiltActivity : BaseActivity<ActivityHiltBinding>() {
    override fun getViewBinding(): ActivityHiltBinding = ActivityHiltBinding.inflate(layoutInflater)

    @Inject
    lateinit var userManager: UserManager

    @Inject
    lateinit var mobilePhone: MobilePhone

    @QualifierConfig.OkHttpClientCall
    @Inject
    lateinit var call: Call

//    @QualifierConfig.OtherOkHttpClientCall
//    @Inject
//    lateinit var call2: Call

    @Inject
    lateinit var retrofit: Retrofit
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //这是没有使用依赖注入的情况,通过实例2个对象来调用内部类的方法
//        val mobilePhone = MobilePhone()
//        mobilePhone.dialNumber()
        //使用依赖注入的情况
//        val mobilePhone = MobilePhone()
//        val simCard = SimCard()
//        mobilePhone.dialNumber(simCard)

//        val userManager = UserManager()//activity不仅负责UI展示,还负责创建UserManager对象
        userManager.getUserToken()
        mobilePhone.dialNumber()

//        val okHttpClient = OkHttpClient()
//        okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS)
//        val request = Request.Builder().url("")
//            .build()
//        val call = okHttpClient.newCall(request)
//        var response = call.execute()

        var response = call.execute()
        var response2 = retrofit.create(Service::class.java)
    }
}

class SimCard @Inject constructor() {
    private val TAG = "SimCard"
    fun dialNumber() {
        Log.i(TAG, "拨打电话")
    }
}

class MobilePhone @Inject constructor(val simCard: SimCard) {
    //没有使用依赖注入的情况
//    fun dialNumber() {
//        val simCard = SimCard()
//        simCard.dialNumber()
//    }
    fun dialNumber() {
        simCard.dialNumber()
    }
}